package com.example.date;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.IsoFields;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
 * @author hej
 * @date 2024/6/3 14:14
 */
public class WeekTest2 {
    public static void main(String[] args) {
        LocalDateTime date = LocalDateTime.of(2025, 1, 5, 5, 12, 12);
        System.out.println(getWeek(date));
        /*System.out.println(date.get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear()));
        System.out.println(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR));

        System.out.println(LocalDate.of(2024, 1, 1).getDayOfWeek().getValue());
        //如果当前年第一天是周一， 那正常计算周，否则周数-1

            //分支 如果周数-1 = 0 ，则说明逆推到上一年， 依然要判断第一天是不是周一，是则正常计算， 否则周数-1

        //一年最后一天取得的周数如果为1， 那么说明周数跨年，需要取得当年最大周数基础上+1


        System.out.println("周数" + date.getYear() + ", " + date.with(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear(),1)
                .plusYears(1).get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear()));
        //初始化，第一周至少四天
        WeekFields wfs= WeekFields.of(Locale.getDefault());
        //一年最后一天日期的LocalDate，如果该天获得的周数为1或52，那么该年就只有52周，否则就是53周
        //获取指定时间所在年的周数
        int num= date.get(wfs.weekOfWeekBasedYear());
        num = num == 1 ? 52 : num;
        System.out.println("第" + num + "周，周一日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.MONDAY));
        System.out.println("第" + num + "周，周二日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.TUESDAY));
        System.out.println("第" + num + "周，周三日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.WEDNESDAY));
        System.out.println("第" + num + "周，周四日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.THURSDAY));
        System.out.println("第" + num + "周，周五日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.FRIDAY));
        System.out.println("第" + num + "周，周六日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.SATURDAY));
        System.out.println("第" + num + "周，周日日期:" + getDateByYearAndWeekNumAndDayOfWeek(2024, num, DayOfWeek.SUNDAY));*/

    }

    private static LocalDate getDateByYearAndWeekNumAndDayOfWeek(Integer year, Integer num, DayOfWeek dayOfWeek) {
        //周数小于10在前面补个0
        String numStr = num < 10 ? "0" + String.valueOf(num) : String.valueOf(num);
        //2019-W01-01获取第一周的周一日期，2019-W02-07获取第二周的周日日期
        String weekDate = String.format("%s-W%s-%s", year, numStr, dayOfWeek.getValue());
        return LocalDate.parse(weekDate, DateTimeFormatter.ISO_WEEK_DATE);
    }

    private static String getWeek(LocalDateTime localDateTime){
        int year = localDateTime.getYear();
        int month = localDateTime.getMonthValue();
        int plusWeek = 0;
        //如果当前年第一天是周一， 那正常计算周，否则周数-1
        if(LocalDate.of(year, 1, 1).getDayOfWeek().getValue() != 1){
            plusWeek = 1;
        }
        //计算周时，默认周一作为一周的开始时间
        int week = localDateTime.get(WeekFields.of(DayOfWeek.MONDAY,1).weekOfWeekBasedYear());
        //如果周数是1， 要判断月份，月份是>=12则说明跨年
        if(week == 1){
            if(month >= 12){
                week = 53;
            }else if(week - plusWeek <= 0){
                //周数是1的另一个情况则是年初，最后一周跨年，周数默认是53，要判断当前年周数是否-1， -1则表示年初跨年，要取上一年的最后一周
                year = year - 1;
                plusWeek = LocalDate.of(year, 1, 1).getDayOfWeek().getValue() != 1 ? 1 : 0;
                week = 53;
            }
        }
        week = week - plusWeek;
        int modelYear = year % 100;
        return (modelYear < 10 ? "0" + modelYear : modelYear) + "" + (week < 10 ? "0" + week : week);
    }
}
